package com.muhuahealth.controller;

import com.muhuahealth.entity.EventRecordEntity;
import com.muhuahealth.entity.RecordTypeEntity;
import com.muhuahealth.service.EventRecordService;
import com.muhuahealth.service.RecordTypeService;
import com.muhuahealth.utils.ServerResponse;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author GSK
 * @version 1.0
 * @date 2021/4/11 17:19
 */

@Log4j2
@Controller
@RequestMapping("/admin")
public class EventRecordController {

    @Autowired
    EventRecordService eventRecordService;

    @Autowired
    RecordTypeService recordTypeService;

    @RequestMapping("/eventRecordCreate")
    public String eventRecordPage(HttpServletRequest request){

        log.info("跳转到事件记录创建页面");
        request.getSession().setAttribute("path","Status_record_create");
        return "Status_record_create";
    }

    @PostMapping("/eventRecordCreate/addNewEventRecord")
    @ResponseBody
    public ServerResponse addNewEventRecord(@RequestBody EventRecordEntity eventEntity){

        log.info("开始添加事件记录");

        try {
                String result = eventRecordService.addNewEventRecord(eventEntity);
                if(result == null){
                    return ServerResponse.createError("插入失败");
                }
        }catch (Exception e){
            e.printStackTrace();
            log.error("发生异常啦:" + e);
        }

        return ServerResponse.createSuccess();
    }

    @GetMapping(value = "/eventRecordCreate")
    public String getTypeCreate(Model model){

        log.info("查询数据类型列表:");

        //获得数据类型列表
        List<RecordTypeEntity> typeList = recordTypeService.getAllTypesByCondition(null);

        System.out.println(typeList);
        model.addAttribute("typeList",typeList);

        return "Status_record_create";
    }

    @GetMapping(value = {"/eventRecordQuery"})
    public String getTypeQuery(Model model){

        log.info("查询数据类型列表:");

        //获得数据类型列表
        List<RecordTypeEntity> typeList = recordTypeService.getAllTypesByCondition(null);

        System.out.println(typeList);
        model.addAttribute("typeList",typeList);

        return "Status_record_query";
    }

    @RequestMapping(value = "/eventRecordQuery/searchRecordInfo",method = RequestMethod.POST)
    @ResponseBody
    public String searchRecordInfo(@RequestBody EventRecordEntity eventEntity,Model model){

        log.info("开始查询数据：");

        List<EventRecordEntity> entityList = eventRecordService.searchRecordInfo(eventEntity);

        if(entityList.size() != 0){
            model.addAttribute("entityList",entityList);
            log.info("eventName:"+eventEntity.getEventName());
            log.info("happenStreet:"+eventEntity.getHappenStreet());
            log.info("eventEntity:" + eventEntity);
            log.info("list=>"+entityList);
            return "Status_record_query::query";
        }

        return null;

    }

    @RequestMapping(value = "/eventRecordQuery/exportExcel", method = RequestMethod.POST)
    @ResponseBody
    public void exportExcel(@RequestBody Map<String, List<String>> map, HttpServletResponse response){

        log.info("开始导出数据");
        eventRecordService.exportExcel(map.get("list"), response);
    }

    @RequestMapping(value = "/eventRecordQuery/exportExcel2", method = RequestMethod.GET)
    @ResponseBody
    public void exportExcel2(HttpServletResponse response)throws IOException {

        log.info("开始导出数据");

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("information");

        List<RecordTypeEntity> list = eventRecordService.exportExcel2();
        //设置要导出的文件名字
        String fileName = "Info" + ".xlsx";

        //新增数据行，并设置单元格数据
        int rowNum = 1;
        //headers表示excel表中第一行的表头
        String[] headers = {"事件类型","事件名称"};

        HSSFRow row = sheet.createRow(0);

        //在excel表中添加表头
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString textString = new HSSFRichTextString(headers[i]);
            cell.setCellValue(textString);
        }

        //在表中存放查询到的数据放入对应的列
        for (RecordTypeEntity recordTypeEntity : list) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(recordTypeEntity.getTypeId());
            row1.createCell(1).setCellValue(recordTypeEntity.getTypeName());
            rowNum++;
        }

        //设置响应头
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition","attachment;filename=" + fileName);
//        response.addHeader("Param","no-cache");
//        response.addHeader("Cache-Control","no-cache");
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
}
